package com.leyou.common.advice;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**@ControllerAdvice       // 自动拦截所有的Controller, 需要引入Spring-webmvc
public class CommonExceptionHandler {
    //该方法的返回值就是将来要返回页面的东西,
    @ExceptionHandler(RuntimeException.class)   // 只拦截RuntimeException异常
    public ResponseEntity<String> handlerException(RuntimeException e){
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());

    }
}
*/
@ControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(LyException.class)
    public ResponseEntity<String> handlerException(LyException e){
        ExceptionEnum em = e.getExceptionEnum();
        return ResponseEntity.status(em.getValue()).body(em.getMessage());

    }
}
